<template>
  <div>
    <el-dialog
      :title="obj.isadd ? '规格添加' : '规格编辑'"
      :visible.sync="obj.istrue"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          v-for="(item, index) in arr"
          :key="index"
        >
          <div class="box">
            <el-input v-model="item.val" label-width="80px"></el-input>
            <el-button type="primary" v-if="index == 0" @click="addinp"
              >新增规格属性</el-button
            >
            <el-button type="warning" v-else @click="delinp(index)"
              >删除按钮</el-button
            >
          </div>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao()">取 消</el-button>
        <el-button type="primary" @click="add" v-if="obj.isadd"
          >添 加</el-button
        >
        <el-button type="primary" @click="bianji()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import { mapActions, mapGetters } from "vuex";
import { specsadd, specsinfo,specsedit } from "../../../request/api";
import { successAlert, warningAlert } from "../../../util/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      dialogVisible: true,
      arr: [{ val: "" }, { val: "" }],
      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
    };
  },
  methods: {
    ...mapActions({
      reqspecslist: "specs/reqchangelist",
      reqspecscount: "specs/reqchangecount",
    }),
    //   点击弹窗x执行的回调函数
    handleClose() {
      this.quxiao();
    },
    // 清空内容
    empty() {
      (this.form = {
        specsname: "",
        attrs: "",
        status: 1,
      }),
        (this.arr = [{ val: "" }, { val: "" }]);
    },
    // 点击取消
    quxiao() {
      this.obj.istrue = false;
      this.empty();
    },
    // 点击添加规格属性
    addinp() {
      this.arr.push({ val: "" });
    },
    // 点击删除规格属性
    delinp(index) {
      this.arr.splice(index, 1);
    },
    // 点击添加规格
    add() {
      this.form.attrs = JSON.stringify(this.arr.map((item) => item.val));
      specsadd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 成功的弹窗
          successAlert(res.data.msg);
          // 关闭当前对话框并清空对话框数据
          this.quxiao();
          // 刷新列表
          this.reqspecslist();
          // 刷新总个数
          this.reqspecscount();
        } else {
          // 失败的弹窗
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条信息
    getinfo(id) {
      specsinfo({ id: id }).then((res) => {
        this.form = res.data.list[0];
        this.arr = JSON.parse(res.data.list[0].attrs).map((item) => ({
          val: item,
        }));
        this.form.id = id;
      });
    },
    // 点击编辑
    bianji() {
      this.form.attrs = JSON.stringify(this.arr.map((item) => item.val));
      console.log(this.form);
      specsedit(this.form).then(res=>{
        if (res.data.code == 200) {
          // 成功的弹窗
          successAlert(res.data.msg);
          // 关闭当前对话框并清空对话框数据
          this.quxiao();
          // 刷新列表
          this.reqspecslist();
        } else {
          // 失败的弹窗
          warningAlert(res.data.msg);
        }
      })
    },
  },
  computed: {
    ...mapGetters({
      specslist: "specs/list",
    }),
  },
  mounted() {
    this.reqspecslist();
  },
};
</script>

<style  lang="less" scoped>
.box {
  display: flex;
}
</style>